package cn.tedu.zblogs.home.controller;


import cn.tedu.base.response.JsonResult;
import cn.tedu.zblogs.home.pojo.VO.ArticleEsVO;
import cn.tedu.zblogs.home.pojo.VO.ArticleVO;
import cn.tedu.zblogs.home.pojo.VO.TagVO;
import cn.tedu.zblogs.home.pojo.dto.ArticleDTO;
import cn.tedu.zblogs.home.service.EsService;
import cn.tedu.zblogs.home.service.HomeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;


@Slf4j
@Api(tags = "首页文章列表")
@RestController
@RequestMapping("/home")
// 解决跨域问题  使用网关的时候需要注释掉
//@CrossOrigin
public class ArticleController {

    @Autowired
    private HomeService homeService;

    @Autowired
    private EsService esService;
    @ApiOperation(value = "查询首页文章列表")
    @GetMapping("article/select")
    public JsonResult selectArticle(ArticleDTO articleDTO){ //@RequestBody ArticleDTO articleDTO
        //ArticleDTO articleDTO = new ArticleDTO();
        System.out.println(articleDTO);
        log.debug("查询首页文章列表");
        List<ArticleVO> list= homeService.SelectArticle(articleDTO);
        return JsonResult.ok(list);

    }

    @ApiOperation(value = "搜索列表")
    @GetMapping("article/selectes")
    public JsonResult selectEsData (Integer page,Integer pageSize,String queryString) throws IOException {
        List<ArticleVO> list = esService.selectArticleEsList(page, pageSize, queryString);
        return JsonResult.ok(list);
    }



    //ES文章入库
    @ApiOperation(value = "ES入库")
    @GetMapping("es")
    public JsonResult testData (Integer id) throws IOException {
        Boolean es = esService.addArticleEsData(id);

        TagVO InsertDataStatistics= new TagVO();
        return JsonResult.ok(InsertDataStatistics);

//        EsUtil esUtil = new EsUtil();
//       esUtil.Test();
//        Map<String, Object> jsonMap = new HashMap<>();
//        //jsonMap.put("name", "spring cloud实战");
//        jsonMap.put("author", "本课程主要从四个章节进行讲解：1.微服务架构入门。2.spring cloud 实战。3.实战spring。4.。。");
////        jsonMap.put("studymodel", "201001");
////        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
////        jsonMap.put("timestamp", dateFormat.format(new Date()));
////        jsonMap.put("price", 5.6f);
//
//        boolean b = esUtil.addData("book_v6", UUID.randomUUID().toString(), jsonMap);
//
//       // System.out.println("Document indexed with ID: " + b);
//
//            // 要分词的文本
//            String text = "IK分词器是一个优秀的中文分词库";
//
//            // 创建分词器对象
//            IKSegmenter segmenter = new IKSegmenter(new StringReader(text), true);
//
//            // 分词
//            Lexeme lexeme = null;
//            try {
//                while ((lexeme = segmenter.next()) != null) {
//                    // 输出分词结果
//                    System.out.println(lexeme.getLexemeText());
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }


        // TagVO InsertDataStatistics= new TagVO();
        // return JsonResult.ok(InsertDataStatistics);
    }

}
